/*
 * modify-item.c
 *
 *  Created on: 21-dic-2008
 *      Author: igor
 */

#include "view.h"

void my_entry_win_modify(void *data, Evas_Object *obj, void *event_info);
static void my_entry_bt_modify(void *data, Evas_Object *obj, void *event_info);
void my_win_modify_item(void *data, Evas_Object *obj, void *event_info);


/* modifica una nota
 *
 * modifies a note
 */
static void
my_entry_bt_modify(void *data, Evas_Object *obj, void *event_info)
{

   printf ("DEBUG: <my_entry_bt_modify>\n");
   struct etodoitem *etdi = data;
   struct todoitem *tdi = (struct todoitem *)malloc(sizeof(struct todoitem));
   sprintf(tdi->inf.title, (char *)elm_entry_entry_get(etdi->inf.title));
   /* allocate memory */
   sprintf(tdi->inf.icon, etdi->inf.icon);
   sprintf(tdi->inf.infos, etdi->inf.infos);
   sprintf(tdi->inf.text, (char *)elm_entry_entry_get(etdi->inf.text));

   mod_item_into_db(tdi); // restituisce NULL se qualcosa non va per il verso giusto

   // Esce dalla funzione
   return;

}

/* modifica una nota
 *
 * modifies an item
 */
void
my_win_modify_item(void *data, Evas_Object *obj, void *event_info)
{
   printf("DEBUG: <my_win_modify_item>\n");

   struct todoitem *tdi = data;
   printf ("DEBUG: titolo (modify) %s ;\n", tdi->inf.title);

   Evas_Object *win, *bg, *bx, *bx2, *bt, *tit, *txt, *en, *sc, *ly;
   char buf[PATH_MAX];

   win = elm_win_add(NULL, PACKAGE " item modify", ELM_WIN_BASIC);
   elm_win_title_set(win, PACKAGE " - item modify");
   elm_win_autodel_set(win, 1);

   bg = elm_bg_add(win);
   elm_win_resize_object_add(win, bg);
   evas_object_size_hint_weight_set(bg, 1.0, 1.0);
   evas_object_show(bg);

   ly = elm_layout_add(win);
   snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
   printf ("DEBUG layout: %s;\n", buf);
   elm_layout_file_set(ly, buf, "modify_item");
   elm_win_resize_object_add(win, ly);
   evas_object_show(ly);

   bx = elm_box_add(win);
   window_swallow(ly, "contenuto_bx", bx);
   evas_object_size_hint_weight_set(bx, 1.0, 1.0);
   evas_object_resize(bx, 320, 300);
   evas_object_show(bx);

   tit = elm_entry_add(win);
   window_swallow(ly, "title_entry", tit);
   elm_entry_line_wrap_set(tit, 0);
   elm_entry_entry_set(tit, tdi->inf.title);
   evas_object_size_hint_weight_set(tit, 1.0, 0.0);
   evas_object_size_hint_align_set(tit, -1.0, -1.0);
   elm_box_pack_end(bx, tit);
   evas_object_show(tit);

   sc = elm_scroller_add(win);
   window_swallow(ly, "title_entry_sc", tit);
   elm_scroller_content_min_limit(sc, 0, 1);
   evas_object_size_hint_weight_set(sc, 1.0, 0.0);
   evas_object_size_hint_align_set(sc, -1.0, -1.0);
   elm_box_pack_end(bx, sc);
   evas_object_show(sc);

   sc = elm_scroller_add(win);
   window_swallow(ly, "text_entry_sc", sc);
   evas_object_size_hint_weight_set(sc, 1.0, 1.0);
   evas_object_size_hint_align_set(sc, -1.0, -1.0);
   elm_box_pack_end(bx, sc);
   evas_object_show(sc);

   txt = elm_entry_add(win);
   window_swallow(ly, "text_entry", txt);
   elm_entry_entry_set(txt, tdi->inf.text);
   evas_object_size_hint_weight_set(txt, 1.0, 1.0);
   evas_object_size_hint_align_set(txt, -1.0, -1.0);
   elm_scroller_content_set(sc, txt);
   evas_object_show(txt);

   struct etodoitem *p = NULL;
   e_todoitem daInserire;
   p = (struct etodoitem *)malloc(sizeof(struct etodoitem));
   daInserire.title = tit;
   daInserire.infos = tdi->inf.infos;
   daInserire.icon = tdi->inf.icon;
   daInserire.text = txt;
   p->inf = daInserire;
   p->pun = NULL;

   /* add a box layout widget to the window */
   bx2 = elm_box_add(win);
   window_swallow(ly, "footer_bx", bx2);
   elm_box_horizontal_set(bx2, 1);
   evas_object_size_hint_weight_set(bx2, 1.0, 1.0);
   evas_object_size_hint_align_set(bx2, -1.0, -1.0);
   evas_object_show(bx2);

   bt = elm_button_add(win);
   window_swallow(ly, "button_save", bt);
   elm_button_label_set(bt, "Save");
   // se esegue prima la chiusura poi non modifica
   evas_object_smart_callback_add(bt, "clicked", my_win_close, win);
   evas_object_smart_callback_add(bt, "clicked", my_entry_bt_modify, p);
   evas_object_size_hint_weight_set(bt, 1.0, 0.0);
   elm_box_pack_end(bx2, bt);
   evas_object_show(bt);

   evas_object_resize(win, 320, 300);

   // FIXME: not exported
   elm_widget_focus_set(win, 1);
   evas_object_show(win);

}

/* modifica una nota esistente
 * scegliere quale nota modificare
 *
 * review an existing item, choose which note change
 * this win displays a list of button with the title of the item as label
 */
void
my_entry_win_modify(void *data, Evas_Object *obj, void *event_info)
{
   Evas_Object *win, *bg, *bx0, *lb, *bx, *bt, *sc, *fr, *ly;
   char buf[PATH_MAX];
   Evas_Object *mother = data;

   win = elm_win_add(NULL, PACKAGE " items modify", ELM_WIN_BASIC);
   /* set the title of the window - this is in the titlebar */
   elm_win_title_set(win, PACKAGE " - items modify");
   elm_win_autodel_set(win, 1);

   bg = elm_bg_add(win);
   /* set weight to 1.0 x 1.0 == expand in both x and y direction */
   evas_object_size_hint_weight_set(bg, 1.0, 1.0);
   /* tell the window that this object is to be resized along with the window.
    * also as a result this object will be one of several objects that
    * controls the minimum/maximum size of the window */
   elm_win_resize_object_add(win, bg);
   /* and show the background */
   evas_object_show(bg);

   ly = elm_layout_add(win);
   snprintf(buf, sizeof(buf), "%s/%s.edj", PIXMAPS_DIR, PACKAGE);
   printf ("DEBUG layout: %s;\n", buf);
   elm_layout_file_set(ly, buf, "modify");
   elm_win_resize_object_add(win, ly);
   evas_object_show(ly);

   Evas_Object *sc_tit;
   /* add a box layout widget to the window */
   bx0 = elm_box_add(win);
   window_swallow(ly, "title_bx", bx0);
   /* allow base box (bx0) to expand in x and y */
   evas_object_size_hint_weight_set(bx0, 1.0, 1.0);
   /* tell the window that the box affects window size and also will be
    * resized when the window is */
   elm_win_resize_object_add(win, bx0);
   evas_object_show(bx0);

   fr = elm_frame_add(win);
   window_swallow(ly, "title_frame", fr);
   elm_frame_label_set(fr, "items");
   elm_box_pack_end(bx0, fr);
   evas_object_show(fr);

   lb = elm_label_add(win);
   elm_label_label_set(lb,
                      "slect a note from the list below by clicking<br>"
					  "the button to modify the todo-item.");
   elm_frame_content_set(fr, lb);
   evas_object_show(lb);

   /* add a scroller object - anything inside is scrollable */
   sc_tit = elm_scroller_add(win);
   window_swallow(ly, "title_sc", sc_tit);
   evas_object_size_hint_weight_set(sc_tit, 1.0, 1.0);
   elm_box_pack_end(bx0, sc_tit);
   evas_object_show(sc_tit);


   /* add a scroller object - anything inside is scrollable */
   sc = elm_scroller_add(win);
   window_swallow(ly, "contenitore_sc", sc);
   /* add scroller to main box */
   evas_object_size_hint_weight_set(sc, 1.0, 1.0);
   evas_object_show(sc);

   Evas_Object *bx_contenitore;
   /* add a box layout widget to the window */
   bx_contenitore = elm_box_add(win);
   // apparentemente nn serve ma meglio temizzare il più possibile
   window_swallow(ly, "contenitore_bx", bx_contenitore);
   /* set weight to 1.0 x 1.0 == expand in x and y) */
   evas_object_size_hint_weight_set(bx_contenitore, 1.0, 0.0);

   /* set the box ad the content in the scrolled view*/
   elm_scroller_content_set(sc, bx_contenitore);
   /* show the box, scroller and main box */
   evas_object_show(bx_contenitore);

   struct todoitem *todolist = NULL;
   todolist = (struct todoitem *) leggi_items_da_db(todolist);

   if (todolist == NULL)
   {
      printf("DEBUG: lista vuota;\n");
   } else {

      while (todolist != NULL)
      {

         bt = elm_button_add(win);
// FIXME - aggiungendo questa chiamata crash della finestra principale
//            window_swallow(ly, "button_item", bt);
         elm_button_label_set(bt, todolist->inf.title);
         evas_object_smart_callback_add(bt, "clicked", my_win_close, win);
         // TODO - verificare quale finestra rimane in vista
  	     evas_object_smart_callback_add(bt, "clicked", my_win_modify_item, todolist);
         evas_object_smart_callback_add(bt, "clicked", my_entry_win_modify, mother);
         /* the button should fill any space it is given horizontally */
         evas_object_size_hint_align_set(bt, -1.0, 0.0);
         /* put the button at the end of the box */
         elm_box_pack_end(bx_contenitore, bt);
         /* show the button */
         evas_object_show(bt);

         // elemento successivo
	     todolist = todolist->pun;

      }
   }

   /* add a box layout widget to the window */
   bx = elm_box_add(win);
   /* set weight to 1.0 x 1.0 == expand in x and y) */
   evas_object_size_hint_weight_set(bx, 1.0, 0.0);

   Evas_Object *bt_quit;
   /* create a button */
   bt_quit = elm_button_add(win);
   /* set the button label */
   window_swallow(ly, "button_quit", bt_quit);
   elm_button_label_set(bt_quit, "Quit");
   /* add a callback to be run when the button is clicked */
   evas_object_smart_callback_add(bt_quit, "clicked", my_win_close, win);
   // FIXME: non refresh correttamente
   evas_object_smart_callback_add(bt_quit, "clicked", my_entry_bt_refresh, mother);

   /* the button should fill any space it is given horizontally */
   evas_object_size_hint_align_set(bt_quit, -1.0, 1.0);
   /* put the button at the end of the box */
   elm_box_pack_end(bx, bt_quit);
   /* show the button */
   evas_object_show(bt_quit);

   /* show the box, scroller and main box */
   evas_object_show(bx);

   /* set an initial window size */
   evas_object_resize(win, 320, 300);
   /* show the window */
   evas_object_show(win);

}
